package GUI;

import 数据库和网络.myDatebase;
import 试题和试卷.subjectiveQuestion;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import 试题和试卷.*;

public class subjectiveQuestionItem extends questionItem{
    JLabel questionInfo, question, answer;
    JScrollPane qScroller,aScroller;
    public JTextArea answerContent;
    int n;
    public subjectiveQuestionItem(int n, question currentquestion) {
        this.n=n;
        this.index=n-1;
        String qContent=currentquestion.getText();
        skipButton.setName(""+n);

        questionInfo = new JLabel("第" + n + "题  简答题");
        question = new JLabel("题目：");
        answer = new JLabel("解答：");

        Font bigFont = new Font("sanserif", Font.BOLD, 24);



        JTextArea questionContent = new JTextArea(6, 20);
        questionContent.setText(qContent);
        questionContent.setLineWrap(true);
        questionContent.setWrapStyleWord(true);
        questionContent.setFont(bigFont);
        questionContent.setEditable(false);

        answerContent = new JTextArea(6, 20);
        answerContent.setLineWrap(true);
        answerContent.setWrapStyleWord(true);
        answerContent.setFont(bigFont);
        answerContent.addFocusListener(new answerListener());

        qScroller = new JScrollPane(questionContent);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        aScroller = new JScrollPane(answerContent);
        aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        questionInfo.setBounds(70, 10, 300, 20);
        qScroller.setBounds(70, 50, 500, 200);
        question.setBounds(20, 50, 50, 30);
        aScroller.setBounds(70,280,500, 200);
        answer.setBounds(20,280,50, 30);

        answerSheet.buttons.add(new JButton(""+n));
        setButton(answerSheet.buttons.get(n));
        int subIndex=n-answerSheet.selectionQuestionNum-answerSheet.definitionQuestionNum-answerSheet.fillingBlankQuestionNum;
        int subRow=subIndex/4+1;
        int subCol=subIndex%4;
        if (subIndex%4==0){
            subRow--;
            subCol=4;
        }
        answerSheet.buttons.get(n).setBounds(50+(subCol-1)*50,440+(answerSheet.selectionRow-1)*40+(answerSheet.definitionRow-1)*40+(answerSheet.fillingRow-1)*40+(subRow-1)*50,40,40);
        answerSheet.mainPanel.add(answerSheet.buttons.get(n));
        answerSheet.mainPanel.updateUI();

        answerSheet.buttons.get(n).addActionListener(new skipListener());
    }

    public void display(){

        test.mainPanel.add(this.aScroller);
        test.mainPanel.add(this.questionInfo);
        test.mainPanel.add(this.question);
        test.mainPanel.add(this.answer);
        test.mainPanel.add(this.qScroller);
    }

    public void remove(){
        test.mainPanel.remove(this.aScroller);
        test.mainPanel.remove(this.questionInfo);
        test.mainPanel.remove(this.question);
        test.mainPanel.remove(this.answer);
        test.mainPanel.remove(this.qScroller);
    }
    public void setButton(JButton button){
        button.setSize(40,40);
        button.setMargin(new Insets(0,0,0,0));
        button.setFont(new Font("宋体",Font.BOLD,14));
        button.setBackground(Color.lightGray);
    }
    public class skipListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e){

            test.demo.dispose();
            test.demo=new test(n);
            test.mainPanel.updateUI();


        }
    }

    public class answerListener implements FocusListener {
        @Override
        public void focusGained(FocusEvent e){
        }
        public void focusLost(FocusEvent e){
            test.resultSet[1][index]=answerContent.getText();
        }
    }

    public void loadAnswer(){
        this.answerContent.setText(test.resultSet[1][index]);
    }
}
